package com.jdrx.sw.sewage.mq;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jdrx.platform.commons.rest.utils.JsonMapper;
import com.jdrx.sw.sewage.service.AlarmDataHandleServiceImpl;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Component
@RocketMQMessageListener(topic = "iot-device-status-smart", consumerGroup = "iot-sewage-device-status-group-smart", consumeThreadMax = 1, consumeMode = ConsumeMode.ORDERLY)
public class DeviceStatusListener implements RocketMQListener<String> {

    private static final Logger logger = LoggerFactory.getLogger(DeviceStatusListener.class);
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private AlarmDataHandleServiceImpl alarmDataHandleServiceImpl;

    @Override
    public void onMessage(String message) {
        List<Map<String, Object>> list = null;
        logger.debug("message:" + message);
        logger.info("收到mq盒子状态数据" + message);
        try {
            if (Objects.isNull(message)) {
                logger.error("盒子状态数据处理失败,message=null");
                return;
            }

            list = objectMapper.readValue(message, List.class);
            if (CollectionUtils.isEmpty(list)) {
                logger.error("盒子状态数据处理失败,data=null");
                return;
            }
            alarmDataHandleServiceImpl.stationNoNetworkOutfcontactStatus(list);
        } catch (Exception e) {
            logger.error("盒子状态数据处理失败,data='{}'", JsonMapper.toJsonString(list), e);
        }
    }
}
